#include    <stdio.h>
 #include<stdlib.h>
 #include<string.h>
 #include    <unistd.h>
int main()
{
int len, i, apipe[2];   //apipe数组中存储两个文件的描述符
char    buf[BUFSIZ];        
 if ( pipe ( apipe ) == -1 ){
    perror("could not make pipe");
    exit(1);
}
printf("Got a pipe! It is file descriptors: { %d %d }\n", 
                        apipe[0], apipe[1]);
while ( fgets(buf, BUFSIZ, stdin) ){  //从标准输入读入数据，放到缓冲区
    len = strlen( buf );
    if (  write( apipe[1], buf, len) != len ){   
        //向apipe[1](即管道写端)写入数据
       perror("writing to pipe");      
        break;                  
    }
    for ( i = 0 ; i<len ; i++ )  //清理缓冲区
        buf[i] = 'X' ;
    len = read( apipe[0], buf, BUFSIZ ) ;   //从apipe[0](即管道读端)读数据   
    if ( len == -1 ){               
        perror("reading from pipe");        
        break;
    }
    if ( write( 1 , buf, len ) != len ){ //把从管道读出的数据再写到标准输出
        perror("writing to stdout");        
        break;                  
    }
}
}
